package com.macro.mall.controller;

import com.macro.mall.dto.OmsMoneyInfoParam;
import com.macro.mall.dto.OmsOrderDeliveryParam;
import com.macro.mall.dto.OmsOrderQueryParam;
import com.macro.mall.dto.OmsReceiverInfoParam;
import com.macro.mall.service.OmsOrderService;
import com.macro.model.OmsOrder;
import common.api.CommonPage;
import common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 *  订单管理
 */
@Api(value = "订单操作管理",tags = "操作模块")
@Controller
@RequestMapping("/order")
public class OmsOrderController {

    @Autowired
    private OmsOrderService omsOrderService;


    @ApiOperation("查询订单")
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<CommonPage<OmsOrder>> list(OmsOrderQueryParam queryParam,
                                                   @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        List<OmsOrder> list = omsOrderService.list(queryParam, pageSize, pageNum);
        CommonPage<OmsOrder> omsOrderCommonPage = CommonPage.restPage(list);
        return CommonResult.success(omsOrderCommonPage);
    }

    @ApiOperation("批量发货")
    @PostMapping("/update/delivery")
    @ResponseBody
    public CommonResult delivery(@RequestBody List<OmsOrderDeliveryParam> deliveryParamList){
        int count = omsOrderService.delivery(deliveryParamList);
        if(count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("批量关闭订单")
    @PostMapping("/update/close")
    @ResponseBody
    public CommonResult close(@RequestParam("ids") List<Long> ids,@RequestParam("note") String note){
        int count = omsOrderService.close(ids,note);
        if(count > 0)
            return CommonResult.success(count);

        return CommonResult.failed();
    }

    @ApiOperation("批量删除订单")
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids){
        int count = omsOrderService.delete(ids);
        if(count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("获取订单详情：订单信息，商品信息，操作记录")
    @GetMapping("/{id}")
    @ResponseBody
    public CommonResult detail(@PathVariable Long id){
        return CommonResult.success(omsOrderService.detail(id));
    }

    @ApiOperation("修改收货人信息")
    @PostMapping("/update/receiverInfo")
    @ResponseBody
    public CommonResult updateReceiverInfo(@RequestBody OmsReceiverInfoParam infoParam){
        int count = omsOrderService.updateReceiverInfo(infoParam);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("修改订单费用")
    @PostMapping("/update/moneyInfo")
    @ResponseBody
    public CommonResult updateMoneyInfo(@RequestBody OmsMoneyInfoParam infoParam){
        int count = omsOrderService.updateMoneyInfo(infoParam);
        if(count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("修改备注信息")
    @PostMapping("/update/note")
    @ResponseBody
    public CommonResult updateNote(@RequestParam("id") Long id,
                                   @RequestParam("status") Integer status,
                                   @RequestParam("note") String note){
        int count = omsOrderService.updateNote(id,note,status);
        if(count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }
}
